package ae.pcfc.etks.online.itworx.ehs.web.web.controller;

import java.io.IOException;
import java.util.Map;

import javax.activation.MimetypesFileTypeMap;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ae.pcfc.etks.online.itworx.ehs.web.constants.GlobalConstants;
import ae.pcfc.etks.online.itworx.ehs.web.delegate.EHSDelegate;
import ae.pcfc.etks.online.itworx.ehs.web.entity.OFCDocument;
import ae.pcfc.etks.online.itworx.ehs.web.exception.InvalidInputException;
import ae.pcfc.etks.online.itworx.ehs.web.util.UtilCommon;

@Controller
@RequestMapping("/common")
public class Common {
	
	private EHSDelegate ehsDelegate;
	private static Logger log = LoggerFactory.getLogger(Officer.class);
	private Map<String, String> generalMap;
	
	@Resource(name = "generalMap")
	public void setGeneralMap(Map<String, String> generalMap) {
		this.generalMap = generalMap;
	}
	
	@Resource(name = "ehsDelegate")
	public void setEhsDelegate(EHSDelegate ehsDelegate) {
		this.ehsDelegate = ehsDelegate;
	}
	
	@RequestMapping(value = "/viewDetails.htm", method = RequestMethod.GET)
	public String getRequestDetails(@RequestParam(value = "requestId") long requestId,
			HttpServletRequest request) {
	
		if(request.isUserInRole(GlobalConstants.OFFICER_ROLE)){
			return "redirect:/officer/viewDetails.htm?type=myRequests&requestId="+requestId;
		}else{// if(request.isUserInRole(GlobalConstants.CUSTOMER_ROLE)){
			return "redirect:/client/viewDetails.htm?type=myRequests&requestId="+requestId;
		}
		
	}
	
	@RequestMapping(value = GlobalConstants.FILE_DOWNLOAD, method = RequestMethod.GET)
	@ResponseBody
	public byte[] download(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "fileId", required = true) String fileId)
			throws IOException {

		// Retrieve file information from DB based on fileId
		// for now we will use test
		OFCDocument ofcDocument = null;
		try {
			ofcDocument = ehsDelegate.findOFCDocumentById(new Long(fileId));
		} catch (NumberFormatException e) {
			log.error("Officer.download() - number format exception while parsing documentId ("+fileId+")",e);
		} catch (InvalidInputException e) {
			log.error("Officer.download() - invalid input provided while downloading document  ("+fileId+")",e);;
		}
		String fullPath = "";
		if (null != ofcDocument) {
			fullPath = generalMap.get("documentLocation") + "/"
					+ ofcDocument.getDocumentLocation();
		}

		org.springframework.core.io.Resource resource = new FileSystemResource(
				fullPath);
		response.setHeader("Cache-Control", "public");
		response.setHeader("Pragma", "public");
		response.setContentType(GlobalConstants.FILE_CONTENT);
		response.setContentLength((int) resource.getFile().length());
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ resource.getFile().getName() + "\"");
		
		response.flushBuffer();
		

		return FileCopyUtils.copyToByteArray(resource.getFile());

	}
}
